package com.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.dto.StudentInfoQueryDTO;
import com.common.domain.vo.StudentInfoVO;
import com.service.serviceInterface.StudentInfoService;
import com.system.entity.StudentInfo;
import com.system.mapper.StudentInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo>
        implements StudentInfoService {
    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Override
    public List<StudentInfoVO> selectSameBuild(StudentInfoQueryDTO dto, long userId) {
        List<StudentInfoVO> infoVOS = studentInfoMapper.selectSameBuildList(userId);
        if (infoVOS.size() <= 0) {
            return new ArrayList<>();
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            infoVOS = infoVOS.stream().filter(i -> i.getName().contains(dto.getName())).collect(Collectors.toList());
        }
        if (StringUtils.isNotBlank(dto.getPhone())) {
            infoVOS = infoVOS.stream().filter(i -> i.getPhone().contains(dto.getPhone())).collect(Collectors.toList());
        }
        if (StringUtils.isNotBlank(dto.getDormId())) {
            infoVOS = infoVOS.stream().filter(i -> i.getDormId().equals(dto.getDormId())).collect(Collectors.toList());
        }
        if (dto.getPageNum() <= 1) {
            infoVOS = infoVOS.stream().limit(dto.getPageSize()).collect(Collectors.toList());
        } else {
            infoVOS = infoVOS.stream().skip(dto.getPageNum() * dto.getPageSize()).limit(dto.getPageSize()).collect(Collectors.toList());
        }
        List<StudentInfoVO> list = infoVOS.stream().sorted(Comparator.comparing(StudentInfoVO::getDormId)).collect(Collectors.toList());
        return list.size() > 0 ? list : new ArrayList<>();
    }

}




